<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../node_modules/vue/dist/vue.global.js"></script>
</head>

<body>
    <div id="app">

        <p>
            <input type="text" v-model="userinfo.firstname" placeholder="姓">
        </p>
        <p>
            <input type="text" v-model="userinfo.lastname" placeholder="名">
        </p>
        <p>
            <input type="text" name="" id="" v-model="userinfo.fullname" placeholder="全名">
        </p>

    </div>
</body>

</html>
<script>
    const app = Vue.createApp({
        data() {
            return {
                userinfo: {
                    firstname: '',
                    lastname: '',
                    fullname: ''
                }
            }
        },
        methods: {

        },
        watch: {
            // firstname(newvalue, oldvalue) {
            //     this.fullname = newvalue + this.lastname
            // },
            // lastname(newvalue, oldvalue) {
            //     this.fullname = this.firstname + newvalue
            // },
            // fullname(newvalue, oldvalue) {
            //     console.log(newvalue);
            //     this.firstname = newvalue.substr(0, 1);
            //     this.lastname = newvalue.substr(1)
            // }

            //监听userinfo 这个对象中的属性变化
            // 使用如下该种方式监听不到userinfo 中的内层数据的变化
            // userinfo(newvalue, oldvalue) {
            //     console.log(newvalue, oldvalue);
            // }

            //深度监听
            userinfo: {
                handler(val) {
                    // val 为改变后的userinfo
                    console.log(val);
                    val.fullname = val.firstname + val.lastname
                },
                deep: true, // 表示深度监听
                immediate: true // 立即马上监听
            }
        }

    }).mount('#app')

</script>